<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />JVM
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/%E9%80%9A%E8%BF%87%E5%AE%9E%E9%AA%8C%E6%9D%A5%E6%AF%94%E8%BE%83git-merge-no-ff-%E4%B8%8E-ff-%E5%92%8C-squash%E7%9A%84%E5%8C%BA%E5%88%AB/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">804 字 ~4分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    <h3 id="实验所需要工具">实验所需要工具</h3>
<ul>
<li>安装 git</li>
<li>图形化工具sourceTree，这个可以帮助我们更直观的观察每种merge方式的过程</li>
</ul>
<h3 id="实验步骤">实验步骤</h3>
<h4 id="1-创建git测试目录并进行初始化">1. 创建git测试目录并进行初始化</h4>
<pre><code># 创建并进入目录
temp % mkdir git_test &amp;&amp; cd git_test

# 初始化git
git_test % git init
Initialized empty Git repository in /Users/domino/temp/git_test/.git/

# 创建并切换 master分支
git_test % git checkout -b master
Switched to a new branch 'master'

# 创建分支初始化文件并提交master分支
git_test % echo hello1 &gt; file1.txt
git_test % git add . &amp;&amp; git commit -am master
[master (root-commit) 1d777ea] master
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
</code></pre>
<h4 id="2--开始">2.  开始</h4>
<ol>
<li>
<p><strong>git merge &ndash;no-ff</strong></p>
<pre><code> # 从master分支 切出 b1分支 
 git_test % git checkout -b b1 master
 Switched to a new branch 'b1'
	
 # 制造两次提交
 git_test % echo hello2 &gt; file2.txt
 git_test % echo hello3 &gt; file3.txt
 git_test % git add . &amp;&amp; git commit -am b1
 [b1 faeb213] b1
  2 files changed, 2 insertions(+)
  create mode 100644 file2.txt
  create mode 100644 file3.txt

 git_test % echo hello4 &gt; file4.txt
 git_test % echo hello5 &gt; file5.txt
 git_test % git add . &amp;&amp; git commit -am b1
 [b1 dbb7416] b1
  2 files changed, 2 insertions(+)
  create mode 100644 file4.txt
  create mode 100644 file5.txt

 # 将b1分支使用 --no-ff 方式merge到master分支
 git_test % git checkout master &amp;&amp; git merge --no-ff b1 -m &quot;Merge branch b1&quot;
 Switched to branch 'master'
 Merge made by the 'recursive' strategy.
  file2.txt | 1 +
  file3.txt | 1 +
  file4.txt | 1 +
  file5.txt | 1 +
  4 files changed, 4 insertions(+)
  create mode 100644 file2.txt
  create mode 100644 file3.txt
  create mode 100644 file4.txt
  create mode 100644 file5.txt
</code></pre>
<p>我们可以看到，以 <code>git merge --no-ff</code> 方式进行合并以后，git的节点图表是如下图所示的。1、2两个节点是每次提交后创建的提交节点，3节点是使用<code>--no-diff</code>进行合并时，创建的提交节点。</p>
<!-- raw HTML omitted -->
<p>从文件上面看，最后的这个节点的变更，是每次提交的变更的一个汇总，是将分支<code>b1</code>的所有的变更，都汇总到一起形成了一个新的提交节点，然后将这个节点提交到<code>master</code> 分支上面去。</p>
<!-- raw HTML omitted -->
</li>
<li>
<p><strong>git merge</strong>
<code>git merge </code> 是默认带 <code>--ff</code> 参数的，也就是<code>fast forward</code>的意思。我们先以实验进行举例，来说明<code>fast forward</code> 是什么意思。</p>
<pre><code># 从master创建一个新分支b2，并切换到b2分支上
git_test % git checkout -b b2 master
Switched to a new branch 'b2'
	
# 创建两次提交文件到b2分支
git_test % echo hello5 &gt; file5.txt
git_test % echo hello6 &gt; file6.txt
git_test % git add . &amp;&amp; git commit -am b2
[b2 79cb433] b2
 1 file changed, 1 insertion(+)
 create mode 100644 file6.txt
	 
git_test % echo hello7 &gt; file7.txt
git_test % echo hello8 &gt; file8.txt
git_test % git add . &amp;&amp; git commit -am b2
[b2 6901550] b2
 2 files changed, 2 insertions(+)
 create mode 100644 file7.txt
 create mode 100644 file8.txt
	 
# 将b2分支直接merge到master分支上面
git_test % git checkout master &amp;&amp; git merge b2 -m &quot;merge branch b2&quot;
Switched to branch 'master'
Updating c72e661..6901550
Fast-forward (no commit created; -m option ignored)
 file6.txt | 1 +
 file7.txt | 1 +
 file8.txt | 1 +
 3 files changed, 3 insertions(+)
 create mode 100644 file6.txt
 create mode 100644 file7.txt
 create mode 100644 file8.txt
</code></pre>
<p>我们可以看到，直接使用<code>git merge</code>，会将整个<code>b2</code>的所有的提交节点，都直接汇入到master分支中。比如说，我有一个开发分支，开发了两周，中间的大大小小的提交有上百次，如果我直接将这个开发分支通过<code>git merge</code>的方式进行提交到<code>master</code>分支的话，那<code>master</code>分支将会被我这个分支中的琐碎的提交所淹没。</p>
<!-- raw HTML omitted -->
<p>拿图中的<code>merge --no-ff</code>与直接<code>merge</code>相比较，我们可以看到， <code>merge --no-ff</code>的合并方式，会合并提交，并保留有原来的提交记录，也不会将原来的提交记录汇入到被合并的分支中，保证了被合并分支的提交记录的整洁，如果有需要进行回滚的点，也可以很方便的从历史提交记录中找到相应的提交点。</p>
<!-- raw HTML omitted -->
<p>而且，就算我们将b1这个分支删除掉，这个记录也不会消失。</p>
<pre><code># 删除b1分支
git_test % git branch -d b1
Deleted branch b1 (was dbb7416).
</code></pre>
<p>我们执行完删除操作以后，可以从下图中看到，分支中的b1分支已经没了，但是历史的提交节点并没有受到影响。</p>
<!-- raw HTML omitted -->
</li>
<li>
<p><strong>merge &ndash;squash</strong>
<code>merge --squash</code> 会将分支的所有的提交压缩到一次提交中，并且不会自动提交，需要<code>merge</code>的操作人手动进行提交。</p>
<pre><code> # 从master分支中创建b3分支，并切换到b3分支上
 git_test % git checkout -b b3 master
 Switched to a new branch 'b3'
	
 # 创建两次文件提交操作
 git_test % echo hello9 &gt; file9.txt
 git_test % echo hello10 &gt; file10.txt
 git_test % git add . &amp;&amp; git commit -am b3
 [b3 c6aa3bc] b3
  2 files changed, 2 insertions(+)
  create mode 100644 file10.txt
  create mode 100644 file9.txt
	 
 git_test % echo hello11 &gt; file11.txt
 git_test % echo hello12 &gt; file12.txt
 git_test % git add . &amp;&amp; git commit -am b3
 [b3 fae0d95] b3
  2 files changed, 2 insertions(+)
  create mode 100644 file11.txt
  create mode 100644 file12.txt
	 
 # 使用 --squash 方式合并b3分支到master
 git_test % git checkout master &amp;&amp; git merge --squash b3
 Switched to branch 'master'
 Updating 6901550..fae0d95
 Fast-forward
 Squash commit -- not updating HEAD
  file10.txt | 1 +
  file11.txt | 1 +
  file12.txt | 1 +
  file9.txt  | 1 +
  4 files changed, 4 insertions(+)
  create mode 100644 file10.txt
  create mode 100644 file11.txt
  create mode 100644 file12.txt
  create mode 100644 file9.txt

 #查看文件状态为未提交状态
 git_test % git status
 On branch master
 Changes to be committed:
   (use &quot;git restore --staged &lt;file&gt;...&quot; to unstage)
     new file:   file10.txt
     new file:   file11.txt
     new file:   file12.txt
     new file:   file9.txt
</code></pre>
<p>进行了以上的<code>merge  --squash</code>操作以后，如下图，我们可以看到，<code>merge --squash</code> 后<code>b3</code>分支与<code>master</code>分支间的关联是中断的，<code>b3</code>分支中提交的文件，在新创建的节点中，也是未提交的状态，在这个步骤操作之前我们说了，<code>merge --squash</code>操作，会将要合并的分支的所有的提交进行合并压缩到一次提交上。</p>
<!-- raw HTML omitted -->
<p>在我们将合并提交以后，我们可以看到，这次提交以后，<code>merge --squash</code>新创建的提交点，与原来的<code>b3</code>分支是断开的，是无法溯源的，如果此时我们将<code>b3</code>这个分支删除掉，那这次提交就变成了</p>
<pre><code> git_test % git commit -am master
 [master ad862bd] master
  4 files changed, 4 insertions(+)
  create mode 100644 file10.txt
  create mode 100644 file11.txt
  create mode 100644 file12.txt
  create mode 100644 file9.txt
</code></pre>
<!-- raw HTML omitted -->
</li>
<li>
<p><strong>merge &ndash;no-ff &ndash;no-commit</strong>
在合并分支时使用<code>--no-commit</code> 参数，可以只执行合并代码的操作，不进行提交操作。<code>--no-ff</code> 配合<code>--no-commit</code> 效果，我们执行一下看看。</p>
<pre><code> # 从master分支创建b4分支，并切换到b4分支
 git_test % git checkout -b b4 master
 Switched to a new branch 'b4'
	
 # 创建变更并进行提交
 git_test % echo hello13 &gt; file13.txt
 git_test % echo hello14 &gt; file14.txt
 git_test % git add . &amp;&amp; git commit -am b4
 [b4 08f9caf] b4
  2 files changed, 2 insertions(+)
  create mode 100644 file13.txt
  create mode 100644 file14.txt
	 
 # 切换到master分支并使用 --no-ff 和--no-commit进行合并 b4分支到master
 git_test % git checkout master &amp;&amp; git merge --no-commit --no-ff b4
 Already on 'master'
 Automatic merge went well; stopped before committing as requested
	
 # 查看文件状态
 git_test % git status
 On branch master
 All conflicts fixed but you are still merging.
   (use &quot;git commit&quot; to conclude merge)
	
 Changes to be committed:
     new file:   file13.txt
     new file:   file14.txt
</code></pre>
<p>从上面的命令的结果和下图，我们可以直观的看到，使用<code>--no-ff --no-commit</code> 的方式进行提交以后，合并是并没有提交的。</p>
<!-- raw HTML omitted -->
<p>我们手动进行提交以后，才提交上去，如下图</p>
<pre><code> git_test % git commit -am master
 [master 8108939] master
</code></pre>
<!-- raw HTML omitted -->
</li>
</ol>
<h3 id="3结论">3.结论</h3>
<p><code>merge --no-ff</code> 与 <code>merge --ff</code> 和<code>merge --squash</code> 相比，拥有可以保持分支整洁，而且可追溯的优点，所以在开发中，推荐使用 <code>merge --no-ff</code> 方式进行分支合并，而且在合并时，配合 <code>--no-commit</code> 进行合并，待检查合并完毕的文件以后再进行提交的方式为最佳方案。</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/%E9%80%9A%E8%BF%87docker%E8%BF%90%E8%A1%8Cvire-benchmark%E5%8E%8B%E6%B5%8B%E9%95%9C%E5%83%8F/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">173</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">5</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">13</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2009 - 2022</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>